package com.clear.honghefinance.admin.controller;

import com.clear.honghefinance.biz.modules.system.role.domain.dto.*;
import com.clear.honghefinance.biz.modules.system.role.domain.vo.GetSysRoleDetailVO;
import com.clear.honghefinance.biz.modules.system.role.domain.vo.ListRoleBindMenuVO;
import com.clear.honghefinance.biz.modules.system.role.domain.vo.ListSysRoleVO;
import com.clear.honghefinance.biz.modules.system.role.service.ISysRoleService;
import com.clear.honghefinance.common.dto.entity.ApiResponse;
import com.clear.honghefinance.common.dto.entity.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

@Api(tags = "角色管理")
@RestController
@RequestMapping(value = "/sysRole")
@Slf4j
public class SysRoleController {
    @Resource
    private ISysRoleService sysRoleService;

    /**
     * 新增角色
     *
     * @param dto req
     * @return
     */
    @ApiOperation(value = "新增角色")
    @PostMapping(value = "/create")
    public ApiResponse<Boolean> create(@Validated @RequestBody CreateSysRoleDTO dto) {
        return ApiResponse.success(sysRoleService.create(dto));
    }

    /**
     * 修改角色
     *
     * @param dto req
     * @return
     */
    @ApiOperation(value = "修改角色")
    @PostMapping(value = "/update")
    public ApiResponse<Boolean> update(@Validated @RequestBody UpdateSysRoleDTO dto) {
        return ApiResponse.success(sysRoleService.update(dto));
    }

    /**
     * 禁用或启用角色
     *
     * @param dto req
     * @return
     */
    @ApiOperation(value = "禁用或启用角色")
    @PostMapping(value = "/updateDisable")
    public ApiResponse<Boolean> updateDisable(@Validated @RequestBody UpdateSysRoleStateDTO dto) {
        return ApiResponse.success(sysRoleService.updateDisable(dto));
    }

    /**
     * 删除角色
     *
     * @param dto req
     * @return
     */
    @ApiOperation(value = "删除角色")
    @PostMapping(value = "/del")
    public ApiResponse<Boolean> del(@Validated @RequestBody DeleteSysRoleDTO dto) {
        return ApiResponse.success(sysRoleService.delete(dto));
    }


    /**
     * 查看角色列表
     *
     * @param dto req
     * @return
     */
    @ApiOperation(value = "查看角色列表")
    @PostMapping(value = "/list")
    public ApiResponse<PageResult<ListSysRoleVO>> list(@Validated @RequestBody ListSysRoleDTO dto) {
        return ApiResponse.success(sysRoleService.list(dto));
    }


    /**
     * 查询角色明细
     *
     * @param id 角色id
     * @return
     */
    @ApiOperation(value = "查询角色明细")
    @GetMapping(value = "getById")
    public ApiResponse<GetSysRoleDetailVO> getById(@Validated @NotNull(message = "角色id不能为空") @RequestParam Integer id) {
        return ApiResponse.success(sysRoleService.getDetailById(id));
    }

    /**
     * 角色绑定菜单
     *
     * @param dto req
     * @return
     */
    @ApiOperation(value = "角色绑定菜单")
    @PostMapping(value = "/roleBindMenu")
    public ApiResponse<Boolean> roleBindMenu(@Validated @RequestBody RoleBindMenuDTO dto) {
        return ApiResponse.success(sysRoleService.roleBindMenu(dto));
    }

    /**
     * 查看当前登录用户角色绑定的菜单列表（用户绑定的角色可能有很多）
     *
     * @return
     */
    @ApiOperation(value = "查询当前登录用户角色绑定的菜单列表")
    @GetMapping(value = "/listRoleBindMenu")
    public ApiResponse<List<ListRoleBindMenuVO>> listRoleBindMenu() {
        return ApiResponse.success(sysRoleService.listRoleBindMenu());
    }

    /**
     * 角色绑定资源
     *
     * @param dto req
     * @return
     */
    @ApiOperation(value = "角色绑定资源")
    @PostMapping(value = "/roleBindResource")
    public ApiResponse<Boolean> roleBindResource(@Validated @RequestBody RoleBindResourceDTO dto) {
        return ApiResponse.success(sysRoleService.roleBindResource(dto));
    }
}